//获取书籍信息
import { ElMessage } from "element-plus";
import { getCurrentInstance } from "vue";

export async function addBookmarkApi(book_id, chapter_index,proxy) {
  const formData = new FormData();
  formData.append("book_id", book_id);
  // formData.append("chapter_id", chapter_id);
  formData.append("chapter_index", chapter_index);
  console.log(book_id, chapter_index,proxy);
  try {
    const response = await proxy.$http.post("/api/book/add-bookmark",formData, {headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
    const res = response.data;
    console.log(res);
    const isSuccess = response.data.success;
    const message = response.data.message;
    if (isSuccess === false) {
      // ElMessage.error(message);
    } else {
      ElMessage.success("添加书签成功");
      return res;
    }
  } catch (error) {
    // console.error(`Error fetching ${book_id} books:`, error);
    return [];
  }
}

export async function getBookmarkApi(book_id) {
  console.log(book_id);
  const { proxy } = getCurrentInstance();
  try {
    const response = await proxy.$http.get("/api/book/get-bookmark", {
      params: { book_id: book_id },
    });
    const res = response.data.data;
    console.log(res);
    const isSuccess = response.data.success;
    const message = response.data.message;
    // console.log(response.data);
    if (isSuccess === false) {
      ElMessage.error(message);
    } else {
      ElMessage.success("查询书签成功");
      return res;
    }
  } catch (error) {
    // console.error(`Error fetching ${id} books:`, error);
    return [];
  }
}

export async function deleteBookmarkApi(bookmark_id,proxy) {
  console.log(bookmark_id,proxy);
  try {
    const response = await proxy.$http.post("/api/book/delete-bookmark", {
      bookmark_id :bookmark_id,
    });
    const res = response.data.data;
    console.log(res);
    const isSuccess = response.data.success;
    const message = response.data.message;
    // console.log(response.data);
    if (isSuccess === false) {
      ElMessage.error(message);
    } else {
      ElMessage.success("删除书签成功");
      return res;
    }
  } catch (error) {
    // console.error(`Error fetching ${id} books:`, error);
    return [];
  }
}
